<?php

namespace app\common\model\order;

use app\common\model\BaseModel;
use think\facade\Db;
use app\common\service\Util;

class GoodsOrder extends BaseModel
{
    protected $name = 'goods_order';

    protected $pk = 'id';
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'add_time';
    protected $updateTime = false;

    protected $type = [
        'add_time' => 'timestamp',
        'update_time' => 'timestamp',
    ];
    
    /**
     * 字段验证规则
     * @var array
     */
    protected $schema = [
        'id'            => 'int',
        'user_id'       => 'int',
        'order_sn'      => 'string',
        'goods_id'      => 'int',
        'goods_name'    => 'string',
        'goods_price'   => 'float',
        'goods_num'     => 'int',
        'total_amount'  => 'float',
        'actual_amount' => 'float',
        'status'        => 'int',
        'add_time'      => 'timestamp',
        'update_time'   => 'timestamp',
        'pay_time'      => 'timestamp',
        'shipping_type' => 'string',
        'shipping_address' => 'string',
        'contact_name'  => 'string',
        'contact_phone' => 'string',
        'delivery_no'   => 'string',
        'delivery_company' => 'string',
        'remark'        => 'string',
    ];

    const STATUS_UN_PAID = 1;
    const STATUS_UN_DELIVERY = 2;
    const STATUS_DELIVERY = 3;
    const STATUS_UN_TAKE = 4;
    const STATUS_TAKE = 5;
    const STATUS_COMPLETE = 6;
    const STATUS_APPLY_REFUND = 7;
    const STATUS_REFUND = 8;
    const STATUS_OPTIONS_ALL  = [
        self::STATUS_UN_PAID     => ['id' => self::STATUS_UN_PAID, 'name' => '待付款'],
        self::STATUS_UN_DELIVERY => ['id' => self::STATUS_UN_DELIVERY, 'name' => '待发货'],
        self::STATUS_DELIVERY    => ['id' => self::STATUS_DELIVERY, 'name' => '待收货'],
        self::STATUS_UN_TAKE     => ['id' => self::STATUS_UN_TAKE, 'name' => '待自提'],
        self::STATUS_TAKE        => ['id' => self::STATUS_TAKE, 'name' => '待评论'],
        self::STATUS_COMPLETE    => ['id' => self::STATUS_COMPLETE, 'name' => '已完成'],
        self::STATUS_APPLY_REFUND => ['id' => self::STATUS_APPLY_REFUND, 'name' => '退款/售后'],
        self::STATUS_REFUND      => ['id' => self::STATUS_REFUND, 'name' => '已退款']
    ];
    const STATUS_OPTIONS = [
        self::STATUS_UN_PAID     => ['id' => self::STATUS_UN_PAID, 'name' => '待付款'],
        self::STATUS_UN_DELIVERY => ['id' => self::STATUS_UN_DELIVERY, 'name' => '待发货'],
        self::STATUS_DELIVERY    => ['id' => self::STATUS_DELIVERY, 'name' => '待收货'],
        self::STATUS_UN_TAKE     => ['id' => self::STATUS_UN_TAKE, 'name' => '待自提'],
        self::STATUS_TAKE        => ['id' => self::STATUS_TAKE, 'name' => '待评论'],
        self::STATUS_COMPLETE    => ['id' => self::STATUS_COMPLETE, 'name' => '已完成'],
        self::STATUS_APPLY_REFUND => ['id' => self::STATUS_APPLY_REFUND, 'name' => '退款/售后'],
        self::STATUS_REFUND      => ['id' => self::STATUS_REFUND, 'name' => '已退款']
    ];


    /**
     * 生成订单号
     * @return string
     */
    public static function buildOrderSn()
    {
        return date('YmdHis') . rand(1000, 9999);
    }

    /**
     * 插入前事件
     * @param $model
     */
    public static function onBeforeInsert($model)
    {
        self::event('before_insert', function ($model) {
            if (empty($model->order_sn)) {
                $model->order_sn = self::buildOrderSn();
            }
        });
    }

    /**
     * 多态关联 - 关联到订单表
     * @return \think\model\relation\MorphOne
     */
    public function order()
    {
        return $this->morphOne(Order::class, 'orderable', 'goods');
    }
}
